<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="商品名称" prop="commodityName">
            <el-input v-model="queryParams.commodityName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="消耗时间">
            <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="materialConsumeList" border>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="物资名称" align="center" prop="commodityName1" />
      <el-table-column label="规格" align="center" prop="commoditySpec" />
      <el-table-column label="单位" align="center" prop="unitName1" />
      <el-table-column label="消耗数量" align="center" prop="consumeNumber" />
      <el-table-column label="消耗金额" align="center" prop="consumeMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.consumeMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleStaffDetails(scope.row)"
            v-hasPermi="['cld:materialConsume:query']">人员消耗明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改消耗记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table :data="staffDetails" border>
        <el-table-column label="姓名" align="center" prop="staffName" />
        <el-table-column label="岗位" align="center" prop="projectPostName">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName" />
          </template>
        </el-table-column>
        <el-table-column label="消耗数量" align="center" prop="totalNumber" />
        <el-table-column label="消耗金额" align="center" prop="totalMoney" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectConsumeList, listMaterialConsume } from "@/api/cld/materialConsume";

export default {
  name: "projectConsumption",
  dicts: ['cld_project_post'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消耗记录表格数据
      materialConsumeList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        commodityName: null
      },
      // 人员消耗明细
      staffDetails: [],
      open: false,
      title: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消耗记录列表 */
    getList() {
      this.loading = true;
      getProjectConsumeList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.materialConsumeList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 人员消耗明细
    handleStaffDetails(row) {
      listMaterialConsume({
        pageNum: 1,
        pageSize: 1000,
        projectId: row.projectId,
        isStaffGroup: 1,
        commodityId: row.commodityId
      }).then(res => {
        this.staffDetails = res.rows;
        this.staffDetails.forEach((i) => {
          let totalNumber = 0;
          let totalMoney = 0;
          i.detailsList.forEach((item) => {
            totalNumber += item.commodityNumber;
            totalMoney += item.commodityNumber * item.costPrice;
          });
          i.totalNumber = totalNumber;
          i.totalMoney = this.toMoney(totalMoney);
        });
        this.title = row.commodityName1;
        this.open = true;
      })
    }
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
